package com.zy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.domain.User;
import com.zy.mapper.UserMapper;
import com.zy.param.PayBindingParam;
import com.zy.param.RegisterParam;
import com.zy.service.UserService;
import com.zy.utils.HashMD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.UUID;

@Service
public  class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

  @Autowired
  private UserMapper userMapper;
  @Override
  public User selectUser(String username, String password) {
//    LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getNickname, username, User::getPassword, password);
//    LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getNickname, username).eq(User::getPassword, password);
//    User user = userMapper.selectOne(eq);
    User user = userMapper.selectOne(new LambdaQueryWrapper<User>().
      eq(User::getNickName, username).
      eq(User::getPassword, password));
    return user;
  }

  @Override
  public User getUserByPhone(String phoneNum) {
//    User user = userMapper.selectOne(new LambdaQueryWrapper<User>.eq(User::getPhoneNum, phoneNum));
//    userMapper.selectOne()
    LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getPhoneNum, phoneNum);
    User user = userMapper.selectOne(eq);
    return user;
  }

  @Override
  public User registerUser(RegisterParam registerParam) {
    registerParam.setPassword(HashMD5Util.getMd5(registerParam.getPassword(),2));
    registerParam.setId(UUID.randomUUID().toString());
    registerParam.setCreateDate(new Date());
    registerParam.setName("");
//    User userByPhone = userMapper.getUserByPhone(registerParam.getPhoneNum());
    User userByPhone = this.selectUserByPhone(registerParam.getParentPhone());
    if (userByPhone==null){
      registerParam.setParentId("");
    }
    else {
      registerParam.setParentId(userByPhone.getParentId());
    }
    registerParam.setLv(0);
    registerParam.setPhotoImage("");
//    registerParam.setTask(0);
    registerParam.setTaskMoney((double) 0);
    registerParam.setXiaofeiMoney((double) 0);
    registerParam.setShouyiMoney((double) 0);
    registerParam.setBaozhengjinMoney((double) 0);
    registerParam.setTuiguangImage("");
    registerParam.setTask(0);
    registerParam.setQiandao(0);
//    registerParam.setQiandaoDate();
    registerParam.setShiming("未实名");
    registerParam.setShimingState("N");
    registerParam.setDongjie("未冻结");
    registerParam.setDongjieState("Y");
    registerParam.setDeleteState("Y");
    registerParam.setBankName("");
    registerParam.setBankNum("");
    registerParam.setBankKaihu("");
    registerParam.setWxName("");
    registerParam.setAliName("");
    int insert = userMapper.insert(registerParam);
    return insert>0?registerParam:null;
  }

  @Override
  public User findUserByusername(String username) {
    LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getNickName, username);
    User user = userMapper.selectOne(eq);
    return user;
  }

  @Override
  public User selectUserByPhone(String phoneNum) {
    LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getPhoneNum, phoneNum);
    User user = userMapper.selectOne(eq);
    return user;
  }

  @Override
  public User resetUserByPhone(String phoneNum) {
    User user = selectUserByPhone(phoneNum);
//    user.setId(UUID.randomUUID().toString());
    user.setName(UUID.randomUUID().toString());
    user.setNickName(UUID.randomUUID().toString());
    user.setParentId("");
    user.setParentPhone("");
    user.setPassword(HashMD5Util.getMd5("123456",2));
//    user.setPhoneNum("");
    user.setLv(0);
    user.setPhotoImage("https://portrait.gitee.com/uploads/avatars/user/1760/5281157_frey6_1594531901.png!avatar200");
    user.setTaskMoney((double) 0);
    user.setXiaofeiMoney((double) 0);
    user.setShouyiMoney((double) 0);
    user.setBaozhengjinMoney((double) 0);
    user.setTuiguangImage("https://portrait.gitee.com/uploads/avatars/user/1760/5281157_frey6_1594531901.png!avatar200");
//    user.setCreateDate();
    user.setUpdateDate(new Date());
    user.setTask(0);
    user.setQiandao(0);
//    user.setQiandaoDate()
    user.setShimingState("N");
    user.setShiming("未实名");
    user.setDongjieState("Y");
    user.setDongjie("未冻结");
    user.setDeleteState("Y");
    user.setBankName("");
    user.setBankNum(UUID.randomUUID().toString());
    user.setBankKaihu("");
    user.setWxName("");
    user.setAliName("");
    userMapper.updateById(user);
    return user;
  }

  @Override
  public User selectUserById(String id) {
    LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getId, id);
    User user = userMapper.selectOne(eq);
    return user;
  }

  @Override
  public User payBinding(PayBindingParam payBindingParam) {
    //用户id
    String userId = payBindingParam.getUserId();
    //姓名
    String name = payBindingParam.getName();
    //开户银行
    String bankName = payBindingParam.getBankName();
    //开户支行
    String bankKaihu = payBindingParam.getBankKaihu();
    //银行卡号
    String bankNum = payBindingParam.getBankNum();
    //支付宝账号
    String aliName = payBindingParam.getAliName();
//    User user=new User();
    User user = this.selectUserById(userId);
//    userMapper.insert(user);
    user.setBankName(bankName);
    user.setBankKaihu(bankKaihu);
    user.setBankNum(HashMD5Util.getMd5(bankNum,2));
    user.setAliName(aliName);
    if (user==null){
      return null;
    }
    int i = userMapper.updateById(user);
    return i>0?user:null;
  }

  @Override
  public User rechargeTaskMoney(String uid, String type, Double money) {
    User user = userMapper.selectById(uid);
    // 充值类型 0 支付宝 1 微信
    user.setTaskMoney(user.getTaskMoney()+money);
    int i = userMapper.updateById(user);
    return i>0?user:null;
  }

  @Override
  public User withdrawalTaskMoney(String uid, String type, Double money) {
    User user = userMapper.selectById(uid);
    // 充值类型 0 支付宝 1 微信 2 银行卡
    Double remainingAmount=user.getTaskMoney()-money;
    if (remainingAmount<0){
      return  null;
    }
    user.setTaskMoney(remainingAmount);
    int i = userMapper.updateById(user);
    return i>0?user:null;

  }

  @Override
  public User withdrawalZhijianMoney(String uid, String type, Double money) {
    User user = userMapper.selectById(uid);
    // 充值类型 0 支付宝 1 微信 2 银行卡
    Double remainingShouyiMoney=user.getShouyiMoney()-money;
    if (remainingShouyiMoney<0){
      return  null;
    }
    user.setTaskMoney(remainingShouyiMoney);
    int i = userMapper.updateById(user);
    return i>0?user:null;
  }

  @Override
  public User signIn(String uid) {
    User user = userMapper.selectById(uid);
    Integer qiandao = user.getQiandao();
    if (qiandao%10==0&&qiandao!=0){
      user.setTaskMoney(user.getTaskMoney()+1);
    }
    user.setQiandaoDate(new Date());
    user.setQiandao(user.getQiandao()+1);
    int i = userMapper.updateById(user);
    return i>0?user:null;
  }
}
